<?php

namespace app\admin\controller\learn;

use app\common\controller\Backend;
use think\Db;
use think\Exception;

/**
 * 组织结构类别
 *
 * @icon fa fa-circle-o
 */
class Orgshare extends Backend
{
    
    /**
     * Orgshare模型对象
     * @var \app\admin\model\learn\Orgshare
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\learn\Orgshare;
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->where(['type'=>3])
                ->count();
            $list = $this->model
                ->where(['type'=>3])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach ($list as $row) {
//                $row->visible(['id','name','origin_price','price','visitor_origin_price','visitor_price','buy_numbers','image','thumbs','is_buy','status','createtime']);

            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /*
     * 添加单次课程列表
     */
    public function add()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        $this->searchFields = 'name';
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->where(['type'=>1])
                ->count();
            $list = $this->model
                ->where(['type'=>1])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /*
    * 设置付费单位
    */
    public function addorg($ids)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if ($params) {
                if(isset($params['ids']) && !empty($params['ids'])){
                    $ids = explode(',', $params['ids']);
                    if(empty($ids)){
                        $this->error('请选择单位！');
                    }
                    $res = $this->model->where('id', 'in', $ids)->update(['type'=>3]);
                    if ($res === false) {
                        $this->error($this->model->getError());
                    }
                    $this->success();
                }
                $this->error('课程编号错误！');
            }
            $this->error();
        }
        return $this->view->fetch();
    }

    /*
    * 删除绑定单次课程
    * @param null $ids
    */
    public function del($ids = null)
    {
        if (!empty($ids)) {
            Db::startTrans();
            try{
                $res = $this->model->where('id', 'in', $ids)->update(['type'=>1]);
                if($res===false){
                    $this->error($this->model->getError());
                }
                $org_courses = Db::name('learn_course')->where('org_cate_id', 0)->column('id');
                if($org_courses){
                    $sys_ids = Db::name('learn_course')->where('org_cate_id', 'in', $ids)->column('id');
                    if($sys_ids){
                        Db::name('learn_course_system')->where('sys_id', 'in', $sys_ids)->where('course_id', 'in', $org_courses)->delete();
                    }
                    Db::name('learn_hot_course')->where('org_cate_id', 'in', $ids)->where('learn_course_id', 'in', $org_courses)->delete();
                    Db::name('learn_cate_course1')->where('org_cate_id', 'in', $ids)->where('learn_cate_id', 4)->where('learn_course_id', 'in', $org_courses)->delete();
                }
                Db::commit();
            }catch (Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }

            $this->success();
        }
        $this->error('编号错误！');
    }

}
